package de.laures.cewolf.util;

import java.awt.Color;
import java.util.LinkedList;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class PropReader {
	public static final PropReader instance = new PropReader();
	private String propertyFileName;
	private ResourceBundle resourceBundle;

	private PropReader() {
		propertyFileName = "style";
		resourceBundle = ResourceBundle.getBundle(propertyFileName);
	}

	public String getString(String key) {
		if (key == null || key.equals("") || key.equals("null")) {
			throw new RuntimeException("properties key:" + key + ",val is null");
		}
		String result = "";
		try {
			result = resourceBundle.getString(key);
		}
		catch (MissingResourceException e) {
			e.printStackTrace();
		}
		return result;
	}

	public int getInteger(String key) {
		String val = this.getString(key);
		if (StringsUtil.isInteger(val)) {
			return StringsUtil.toInt(val);
		}
		else {
			throw new RuntimeException("properties key:" + key + ",val is not int");
		}
	}

	public float getFloat(String key) {
		String val = this.getString(key);
		if (StringsUtil.isNumber(val)) {
			return Float.valueOf(val);
		}
		else {
			throw new RuntimeException("properties key:" + key + ",val is not float");
		}
	}

	public double getDouble(String key) {
		String val = this.getString(key);
		if (StringsUtil.isNumber(val)) {
			return Double.valueOf(val);
		}
		else {
			throw new RuntimeException("properties key:" + key + ",val is not double");
		}
	}

	public boolean getBoolean(String key) {
		String val = this.getString(key);
		return Boolean.valueOf(val);
	}

	public Color getColor(String key) {
		String val = this.getString(key);
		try {
			return parseColor(val);
		}
		catch (Exception e) {
			throw new RuntimeException("properties key:" + key + ",val is not color");
		}
	}

	public List<Color> getColorList(String key) {
		String val = this.getString(key);
		try {
			List<Color> list = new LinkedList<Color>();
			String[] strs = val.split(",");
			for (String str : strs) {
				list.add(parseColor(str));
			}
			return list;
		}
		catch (Exception e) {
			throw new RuntimeException("properties key:" + key + ",val is not color list");
		}
	}

	public Color parseColor(String val) {
		int colorInt = Integer.parseInt(val, 16);
		int r = ((colorInt >> 16) & 255);
		int g = ((colorInt >> 8) & 255);
		int b = ((colorInt >> 0) & 255);
		return new Color(r, g, b);
	}
}
